package handler

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"post-hub/inits"
	"post-hub/internal/dto"
	"post-hub/internal/model"
	"post-hub/response"
)

func Follow(c *gin.Context) {

	var follow model.Follow
	err := c.ShouldBind(&follow)
	if err != nil {
		response.Error(c, "解析失败")
		return
	}
	follow = model.Follow{
		UserId: follow.UserId,
		DistId: follow.DistId,
	}
	err = dto.CreateFollow(&follow)
	if err != nil {
		response.Error(c, "关注失败")
		return
	}
	response.Success(c, "关注成功", follow)
	inits.DB.Begin()
	var user model.User
	err = c.ShouldBind(&user)
	if err != nil {
		response.Error(c, "解析失败")
		return
	}
	user = model.User{
		Model: gorm.Model{
			ID: uint(follow.UserId),
		},
		Fans: user.Fans + 1,
	}
	dto.UpdateUser(&user, int(user.ID))
	inits.DB.Commit()
}
